package com.fingard.dsp.bank.directbank.ccb02;

import java.io.File;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IElecBillTrans;
import com.fingard.dsp.bank.reqrespobj.elecbilltrans.*;
import com.fingard.dsp.bank.reqrespobj.elecbilltrans.detail.*;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/9/11.
 */
public class CCB02ElecBillTrans extends CCB02Base implements IElecBillTrans {

    /**
     * 通用签收 b2e0109
     */
    @Override
    public RetAtsB003BillRespSubmit D_B003_BillResp_Submit(ReqB003BillRespSubmit query) throws Exception {
        RetAtsB003BillRespSubmit retAts = new RetAtsB003BillRespSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        while (query.readRecord()){
            RetAtsB003BillRespSubmitDetail detail = new RetAtsB003BillRespSubmitDetail();
            try {
                CCB02Header header = new CCB02Header();
                header.REQUEST_SN =getQryTransID();
                header.TX_CODE = "6WH014";
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                writeCommonHeader(xmlTextWriter,header);
                xmlTextWriter.writeStartElement("TX_INFO");
//                BkType1 回复类型 Char(4) F 0001：承兑回复
//                0002：出票回复
//                0003：背书回复
//                0004：贴现回复
//                0005：贴现赎回回复
//                0006：转贴现回复
//                0007：转贴现赎回回复
//                0009：再贴现赎回回复
//                0010：央行卖票回复
//                0011：保证回复
//                0012：质押回复
//                0013：质押解除回复
//                0014：提示付款回复
//                0015：逾期提示付款回复
//                0016：同意清偿回复
//                网银可以回复：0001、0002、0003、0005、0011、0012、0013、0014、0015、0016
//                CP+CBD可以回复：
//                0001、0004、0006、0007、0009、0010、0011、0012、0013、0014、0015、0016
                String bkType;
                switch (Integer.valueOf(query.detail.respType)){
                    //1-承兑应答 2-收票应答 3-背书转让应答 4-回购式贴现赎回应答 5-质押解除应答 6-提示付款应答 7-保证应答 8-同意清偿应
                    case 1:bkType ="0001";break;
                    case 2:bkType ="0002";break;
                    case 3:bkType ="0003";break;
                    case 4:bkType ="0005";break;
                    case 5:bkType ="0013";break;
                    case 6:
                        if("1".equals(query.detail.proPayType)) {
                            bkType = "0014";
                        }else{
                            bkType = "0015";
                        }break;
                    case 7:throw new Exception("银行暂不支持保证类型的签收");
                    case 8:bkType ="0016";break;
                    case 9:bkType ="0012";break;
                    default:throw new Exception("未知的签收类型："+query.detail.respType);
                }
                xmlTextWriter.writeElementString("BkType1",bkType);//BkType1
//                8 BkListNo1 电子票据号码 Char(30) F 　
                xmlTextWriter.writeElementString("BkListNo1",query.detail.billNo);
//                9 BkRole 回复人类别 Char(4) F RC00:接入行行
//                RC01:企业
//                RC02:人民银行
//                RC03:被代理行
//                RC04:被代理财务公司
//                RC05:接入财务公司
                String bkRole = getBankConfig().getMapValueByKey("signRole",query.detail.actNum);
                xmlTextWriter.writeElementString("BkRole",bkRole);
//                10 BkAcctNo1 回复人账号 varChar(32) F 如发起方为我行，填写一个‘0’；其他情况下，填写回复人账号
                xmlTextWriter.writeElementString("BkAcctNo1",query.detail.actNum);
//                11 BkBrchNo1 回复人开户行行号 varChar(12) F 　
                xmlTextWriter.writeElementString("BkBrchNo1",query.detail.respBnkCode);
//                12 BkBrchNo2 回复人承接行行号 varChar(12) T 　
                xmlTextWriter.writeElementString("BkBrchNo2","");
                if("1".equals(query.detail.respType)) {
//                13 BkLevel1 承兑人信用等级 varChar(3) T 承兑签收时为可选项，其余签收时为不填写项
                    xmlTextWriter.writeElementString("BkLevel1", query.detail.accpCredit);
//                14 BkPrtFileName 承兑人评级机构 varChar(120) T 承兑签收时为可选项，其余签收时为不填写项
                    xmlTextWriter.writeElementString("BkPrtFileName", query.detail.accpGrade);
//                15 Bk8Date1 承兑人评级到期日 Char(8) T 承兑签收时为可选项，其余签收时为不填写项
                    xmlTextWriter.writeElementString("Bk8Date1", DateHelper.convertD10toD8(query.detail.accpGradeDue));
                }else{
//                    13 BkLevel1 承兑人信用等级 varChar(3) T 承兑签收时为可选项，其余签收时为不填写项
                    xmlTextWriter.writeElementString("BkLevel1", "");
//                14 BkPrtFileName 承兑人评级机构 varChar(120) T 承兑签收时为可选项，其余签收时为不填写项
                xmlTextWriter.writeElementString("BkPrtFileName","");
//                15 Bk8Date1 承兑人评级到期日 Char(8) T 承兑签收时为可选项，其余签收时为不填写项
                    xmlTextWriter.writeElementString("Bk8Date1", "");
                }
//                16 BkAddr 保证人地址 varChar(512) T 保证签收时是必填项
                xmlTextWriter.writeElementString("BkAddr",query.detail.guarAddr);
//                17 BkListNo4 协议编号 varChar(30) T 承兑、贴现时候填写
                if("1".equals(query.detail.respType)) {
                    xmlTextWriter.writeElementString("BkListNo4", query.detail.protocolNo);
                }else{
                    xmlTextWriter.writeElementString("BkListNo4", "");
                }
//                18 BkType2 回复标记 Char(4) F SU00同意签收
//                SU01拒绝签收
                xmlTextWriter.writeElementString("BkType2","1".equals(query.detail.respFlag)?"SU00":"SU01");


                if("6".equals(query.detail.respType)&&"2".equals(query.detail.respFlag)) {
                    if(StringHelper.isNullOrEmpty(query.detail.unPayCode)){
                        throw new Exception("提示付款拒签时，拒付理由不能为空!");
                    }
                    if("99".equals(query.detail.unPayCode)){
                        xmlTextWriter.writeElementString("BkType3", "DC09");
                    }else{
                        xmlTextWriter.writeElementString("BkType3", "DC0" + (Integer.valueOf(query.detail.unPayCode) - 1));
                    }
                }else{
                    xmlTextWriter.writeElementString("BkType3", "");
                }
                /*if(StringHelper.hasAnyChar(query.detail.unPayCode)){
                    String noPayReason;
                    switch (Integer.valueOf(query.detail.unPayCode)){
                        case 1:noPayReason = "DC00";break;
                        case 2:noPayReason = "DC01";break;
                        case 3:noPayReason = "DC02";break;
                        case 4:noPayReason = "DC03";break;
                        case 5:noPayReason = "DC04";break;
                        case 6:noPayReason = "DC05";break;
                        case 7:noPayReason = "DC06";break;
                        case 8:noPayReason = "DC07";break;
                        case 9:noPayReason = "DC08";break;
                        case 99:noPayReason ="DC09";break;
                        default:throw new Exception("未知的拒付理由代码："+query.detail.unPayCode);
                    }
                    if(StringHelper.hasAnyChar(noPayReason)){
                        xmlTextWriter.writeElementString("BkType3",noPayReason);//拒付理由(2)
                        //                <noPayRemark></noPayRemark>
                        if("DC09".equals(noPayReason)) {
                            xmlTextWriter.writeElementString("BkDetail1", query.detail.unPayMemo);//拒付备注
                        }
                    }
                }*/
                String[] ecdsInfo = query.detail.busiNo.split("_");
                if(StringHelper.hasAnyChar(query.detail.busiNo)){
                    //                20 BkOldPlatDate 原ECDS平台日期 Char(8) F 　
                    xmlTextWriter.writeElementString("BkOldPlatDate",ecdsInfo[0]);
//                21 BkOldSeq 原ECDS平台流水 Int F 　
                    xmlTextWriter.writeElementString("BkOldSeq",ecdsInfo[1]);
                }else {
                    detail.respCode = "-1";
                    detail.respInfo = "原ECDS平台流水异常。请确认应答查询是否返回BkOldPlatDate和BkOldSeq的值";
                    WriteBankLogLn("原ECDS平台流水异常。请确认应答查询是否返回BkOldPlatDate和BkOldSeq的值");
                    retAts.writeDetail(detail);
                    retAts.writeEnd();
                    return retAts;
                }
                //拒付备注
                xmlTextWriter.writeElementString("BkDetail1", query.detail.unPayMemo);
//                23 BkDetail2 回复人备注 varChar(512) T 　
                xmlTextWriter.writeElementString("BkDetail2",query.detail.memo+"-"+query.detail.reqSeqID);
//                24 BkElctSgn1 回复人电子签名 varChar(4000) T 网银必填
                xmlTextWriter.writeElementString("BkElctSgn1",query.detail.respName);
//                28 SIGN_INFO varChar(255) 证书签名 F
                xmlTextWriter.writeEndElement();
                xmlTextWriter.writeElementString("SIGN_INFO","");
                xmlTextWriter.writeEndDocument();
                String [] result = sendToBank(xmlTextWriter.toXmlString());
                if(result[0].length() ==0){
                    StringBuilder tmpsbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1],tmpsbMsg);
                    if(tmpBkReader!=null){
                        String returnCode = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                        //测试
                        //returnCode= "000000";
                        if(StringHelper.hasAnyChar(returnCode )&&"000000".equals(returnCode)) {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                            detail.reqSeqID =query.detail.reqSeqID;
                            detail.transState = returnCode;
                            detail.payInfoCode = detail.transState;
                        }else {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                            detail.reqSeqID =query.detail.reqSeqID;
                            detail.transState = returnCode;
                            detail.payInfoCode = detail.transState;
                        }
                    }else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpsbMsg.toString();
                    }
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = result[1];
                }
            }catch (Exception ex){
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB101BillApplySubmit D_B101_BillResp_Submit(ReqB101BillApplySubmit query) throws Exception {
        RetAtsB101BillApplySubmit retAts = new RetAtsB101BillApplySubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        query.readRecord();
        RetAtsB101BillApplySubmitDetail detail;
        if("1".equals(query.detail.billType)){
            detail = B101_Submit_AC01(query);
        }else{
            detail = B101_Submit_ACO2(query);
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    public RetAtsB101BillApplySubmitDetail B101_Submit_ACO2(ReqB101BillApplySubmit query){
        RetAtsB101BillApplySubmitDetail detail = new RetAtsB101BillApplySubmitDetail();
        try {
            CCB02Header header = new CCB02Header();
            header.REQUEST_SN =getQryTransID();
            header.TX_CODE = "6WH003";
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            writeCommonHeader(xmlTextWriter,header);
            xmlTextWriter.writeStartElement("TX_INFO");
//            BkAmt1 票据金额 Decimal（18.2) F 　
            xmlTextWriter.writeElementString("BkAmt1",query.detail.billAmt);
//            8 Bk8Date1 出票日期 varChar(8) F 　
            xmlTextWriter.writeElementString("Bk8Date1",query.detail.issDate.substring(0,10).replace("-",""));
//            9 Bk8Date2 到期日 varChar(8) F 　
            xmlTextWriter.writeElementString("Bk8Date2",query.detail.dueDate.substring(0,10).replace("-",""));
//            10 BkType2 不得转让标记 char(4) F EM00可再转  EM01不得转让
            String bkType;
            if("1".equals(query.detail.transFlag)){
                bkType = "EM00";
            }else {
                bkType = "EM01";
            }
            xmlTextWriter.writeElementString("BkType2",bkType);
//            11 Bk60Name1 出票人名称 varChar(120) F 　
            xmlTextWriter.writeElementString("Bk60Name1",query.detail.drawName);
//            12 BkElctSgn1 出票人电子签名 varChar(4000) F 　
            xmlTextWriter.writeElementString("BkElctSgn1",query.detail.drawName);//// TODO: 2018/9/11
//            13 BkAcctNo1 出票人账号 varChar(32) F 　
            xmlTextWriter.writeElementString("BkAcctNo1",query.detail.drawAct);
//            14 BkBrchNo1 出票人开户行行号 char(12) F 　
            xmlTextWriter.writeElementString("BkBrchNo1",query.detail.drawBnkCode);
//            15 BkLevel1 出票人信用等级 char(3) T 　
            xmlTextWriter.writeElementString("BkLevel1",query.detail.creditLv);
//            16 Bk60Name2 出票人评级机构 varChar(120) T 　
            xmlTextWriter.writeElementString("Bk60Name2",query.detail.gradeAgent);
//            17 Bk8Date3 出票人评级到期日 char(8) T 　
            if(StringHelper.hasAnyChar(query.detail.gradeDueDate)) {
                xmlTextWriter.writeElementString("Bk8Date3", query.detail.gradeDueDate.substring(0,10).replace("-",""));
            }else{
                xmlTextWriter.writeElementString("Bk8Date3", "");
            }
//            18 BkDetail1 出票人承诺 varChar(60) T 　
            xmlTextWriter.writeElementString("BkDetail1","");
//            19 BkDetail2 出票人备注 varChar(512) T 　
            xmlTextWriter.writeElementString("BkDetail2",query.detail.memo+"_"+query.detail.reqSeqID);
//            20 Bk60Name3 承兑人名称 varChar(120) F 　
            xmlTextWriter.writeElementString("Bk60Name3",query.detail.accpName);
//            21 BkAcctNo3 承兑人账号 varChar(32) F 　
            xmlTextWriter.writeElementString("BkAcctNo3",query.detail.accpAct);
//            22 BkBrchNo3 承兑人开户行行号 char(12) F 　
            xmlTextWriter.writeElementString("BkBrchNo3",query.detail.accpBnkCode);
//            23 Bk60Name4 收款人名称 varChar(120) F 　
            xmlTextWriter.writeElementString("Bk60Name4",query.detail.payeeName);
//            24 BkAcctNo4 收款人账号 varChar(32) F 　
            xmlTextWriter.writeElementString("BkAcctNo4",query.detail.payeeAct);
//            25 BkBrchNo4 收款人开户行行号 char(12) F 　
            xmlTextWriter.writeElementString("BkBrchNo4",query.detail.payeeBnkCode);
//            26 BkDetail3 备注2 varChar(255) 0 出库号/入库号
            xmlTextWriter.writeElementString("BkDetail3","");
            xmlTextWriter.writeEndElement();
//            27 SIGN_INFO 证书签名 varChar(254) T
            xmlTextWriter.writeElementString("SIGN_INFO","");
            xmlTextWriter.writeEndDocument();
            String [] result = sendToBank(xmlTextWriter.toXmlString());
            if(result[0].length() ==0){
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1],tmpsbMsg);
                if(tmpBkReader!=null){
                    String returnCode = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                    //returnCode= "000000";
                    if("000000".equals(returnCode)) {
                        detail.respCode = "0";
                        detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                        detail.reqSeqID =query.detail.reqSeqID;
                        detail.transState = returnCode;
                        detail.payInfoCode = detail.transState;
                    }else {
                        detail.respCode = "0";
                        detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                        detail.reqSeqID =query.detail.reqSeqID;
                        detail.transState = returnCode;
                        detail.payInfoCode = detail.transState;
                    }
                }else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpsbMsg.toString();
                }
            }else{
                detail.respCode = "-1";
                detail.respInfo = result[1];
            }
        }catch (Exception ex){
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        return detail;
    }

    public RetAtsB101BillApplySubmitDetail B101_Submit_AC01(ReqB101BillApplySubmit query){
        RetAtsB101BillApplySubmitDetail detail = new RetAtsB101BillApplySubmitDetail();
        try {
            CCB02Header header = new CCB02Header();
            header.REQUEST_SN =getQryTransID();
            header.TX_CODE = "6WH002";
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            writeCommonHeader(xmlTextWriter,header);
            xmlTextWriter.writeStartElement("TX_INFO");
//            BkListNo1            协议编号             varChar(40) F 协议编号（即CLPM合同编号）下可能存在多笔待出票的电票信息，网银一次提交。我行ECDS会进行逐一出票处理
            XmlTextReader xmlTextReader = queryBillBy6WH024(query.detail.busiNo);
            String totalnum = xmlTextReader.getFirstText("/TX/TX_INFO/BkNum1");
            xmlTextWriter.writeElementString("BkListNo1",query.detail.busiNo);
//            8 BkLevel1             出票人信用等级       varChar3) T 　
            xmlTextWriter.writeElementString("BkLevel1",query.detail.creditLv);
//            9 Bk60Name2            出票人评级机构 varChar(120) T 　
            xmlTextWriter.writeElementString("Bk60Name2",query.detail.gradeAgent);
//            10 Bk8Date3             出票人评级到期日 varChar(8) T 　
            String gradeDueDat = StringHelper.hasAnyChar(query.detail.gradeDueDate)?
                    query.detail.gradeDueDate.substring(0,10):query.detail.gradeDueDate;
            xmlTextWriter.writeElementString("Bk8Date3",gradeDueDat);
//            11 SIGN_INFO 证书签名 varChar(254) T 　
            xmlTextWriter.writeElementString("SIGN_INFO","");
//            12 BkNum1               出票循环数           int 　 　
            xmlTextWriter.writeElementString("BkNum1",totalnum);
//            循环信息开始
            xmlTextWriter.writeStartElement("DETAILLIST");
            while (xmlTextReader.read("/TX/TX_INFO/DETAILLIST/DETAILINFO")) {
                xmlTextWriter.writeStartElement("DETAILINFO");
//            13 BkListNo2            CLPM票据编号         varChar(10) F 　
                xmlTextWriter.writeElementString("BkListNo2", xmlTextReader.readedXElement.getSingleText("BkListNo1"));
//            14 BkListNo3            交易合同编号         varChar(30) T 　
                xmlTextWriter.writeElementString("BkListNo3", xmlTextReader.readedXElement.getSingleText("BkListNo3"));
//            15 BkListNo4            发票号码             varChar(30) T 　
                xmlTextWriter.writeElementString("BkListNo4", xmlTextReader.readedXElement.getSingleText("BkListNo4"));
//            16 BkEcdBatchNo         批次号               varChar(9) T 　
                xmlTextWriter.writeElementString("BkEcdBatchNo", "");
//            17 Bk8Date1             出票日               varChar(8) F 　
                xmlTextWriter.writeElementString("Bk8Date1", query.detail.issDate.substring(0, 10).replace("-", ""));
//            18 Bk8Date2             票据到期日           varChar(8) F 　
                xmlTextWriter.writeElementString("Bk8Date2", query.detail.dueDate.substring(0, 10).replace("-", ""));
//            19 BkDetail2            备注                 varChar(512) T 　
                xmlTextWriter.writeElementString("BkDetail2", xmlTextReader.readedXElement.getSingleText("BkListNo1" )+ "_" + query.detail.reqSeqID);
//            20 BkType1              不得转让标记 int F EM00可再转让 EM01不得转让
                xmlTextWriter.writeElementString("BkType1", "EM00");
                xmlTextWriter.writeEndElement();
            }
            xmlTextWriter.writeEndDocument();
            String [] result = sendToBank(xmlTextWriter.toXmlString());
            if(result[0].length() ==0){
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1],tmpsbMsg);
                if(tmpBkReader!=null){
                    String returnCode = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                    //测试
                    //returnCode= "000000";
                    if(StringHelper.hasAnyChar(returnCode )&&"000000".equals(returnCode)) {
                        detail.respCode = "0";
                        detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                        detail.reqSeqID =query.detail.reqSeqID;

                        detail.transState = returnCode;
                        detail.payInfoCode = detail.transState;
                    }else {
                        detail.respCode = "0";
                        detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                        detail.reqSeqID =query.detail.reqSeqID;
                        detail.transState = returnCode;
                        detail.payInfoCode = detail.transState;
                    }
                }else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpsbMsg.toString();
                }
            }else{
                detail.respCode = "-1";
                detail.respInfo = result[1];
            }
        }catch (Exception ex){
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        return detail;
    }

    @Override
    public RetAtsB102BillAccpSubmit D_B102_BillResp_Submit(ReqB102BillAccpSubmit query) throws Exception {
        RetAtsB102BillAccpSubmit retAts = new RetAtsB102BillAccpSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        query.readRecord();
            RetAtsB102BillAccpSubmitDetail detail = new RetAtsB102BillAccpSubmitDetail();
            try {
                XElement billInfo = getBillInfo(query.detail.billNo);
                CCB02Header header = new CCB02Header();
                header.REQUEST_SN =getQryTransID();
                header.TX_CODE = "6WH018";
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                writeCommonHeader(xmlTextWriter,header);
                xmlTextWriter.writeStartElement("TX_INFO");
//                BkListNo1 varChar(30) 电子票据号码 F 　
                xmlTextWriter.writeElementString("BkListNo1",query.detail.billNo);
//                11 BkListNo2 varChar(30) 交易合同编号 T 　
                xmlTextWriter.writeElementString("BkListNo2","");
//                12 BkListNo3 varChar(30) 发票号码 T 　
                xmlTextWriter.writeElementString("BkListNo3","");
//                13 BkEcdBatchNo varChar(10) 批次号 T 　
                xmlTextWriter.writeElementString("BkEcdBatchNo","");
//                14 BkDetail1 varChar(512) 出票人备注 T 　
                xmlTextWriter.writeElementString("BkDetail1",query.detail.memo+"_"+query.detail.reqSeqID);
//                17 BkElctSgn1 varChar(4000) 出票人电子签名 F 　
                xmlTextWriter.writeElementString("BkElctSgn1",query.detail.drawName);
//                18 Bk60Name1 varChar(120) 出票人名称 F 　 　
                xmlTextWriter.writeElementString("Bk60Name1",query.detail.drawName);
//                19 BkAcctNo1 varChar(32) 出票人账号 F 　
                xmlTextWriter.writeElementString("BkAcctNo1",query.detail.actNum);
//                20 BkBrchNo1 varChar(12) 出票人开户行行号 F 　
                xmlTextWriter.writeElementString("BkBrchNo1",billInfo.getSingleText("BkBrchNo1"));
                xmlTextWriter.writeEndElement();
//                28 SIGN_INFO varChar(255) 证书签名 F
                xmlTextWriter.writeElementString("SIGN_INFO","");
                xmlTextWriter.writeEndDocument();
                String [] result = sendToBank(xmlTextWriter.toXmlString());
                if(result[0].length() ==0){
                    StringBuilder tmpsbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1],tmpsbMsg);
                    if(tmpBkReader!=null){
                        String returnCode = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                        if("000000".equals(returnCode)) {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                            detail.reqSeqID =query.detail.reqSeqID;
                            detail.transState = returnCode;
                            detail.payInfoCode = detail.transState;
                        }else {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                            detail.reqSeqID =query.detail.reqSeqID;
                            detail.transState = returnCode;
                            detail.payInfoCode = detail.transState;
                        }
                    }else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpsbMsg.toString();
                    }
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = result[1];
                }
            }catch (Exception ex){
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);

        retAts.writeEnd();
        return retAts;
    }

    /**
     * 获取票据的信息
     * @param billNo
     * @return
     */
    private XElement getBillInfo(String billNo)throws Exception{
        XmlTextWriter xmlTextWriter = new XmlTextWriter();
        CCB02Header header = new CCB02Header();
        header.REQUEST_SN = getQryTransID();
        header.TX_CODE = "6WH049";
        writeCommonHeader(xmlTextWriter, header);
        //                <body>
        xmlTextWriter.writeStartElement("TX_INFO");
        xmlTextWriter.writeStartElement("DETAILLIST");
        xmlTextWriter.writeStartElement("DETAILINFO");
        xmlTextWriter.writeElementString("BkListNo10", billNo);
        xmlTextWriter.writeEndDocument();
        String[] result = sendToBank(xmlTextWriter.toXmlString());
        if (result[0].length() == 0) {
            StringBuilder tmpsbMsg = new StringBuilder();
            XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1], tmpsbMsg);
            if (tmpbkReader != null) {
                String returnCode = tmpbkReader.getFirstText("/TX/RETURN_CODE");
                String returnMsg = tmpbkReader.getFirstText("/TX/RETURN_MSG");
                if (StringHelper.hasAnyChar(returnCode) && "000000".equals(returnCode)) {
                    String bknum = tmpbkReader.getFirstText("/TX/TX_INFO/BkNum1");
                    if (!"1".equals(bknum)) {
                       throw new Exception("获取票据明细失败");
                    }
                    if(tmpbkReader.read("/TX/TX_INFO/DETAILLIST/DETAILINFO")){
                        return tmpbkReader.readedXElement;
                    }else {
                        throw new Exception("解析票据节点失败");
                    }
                }else{
                    throw new Exception(returnMsg);
                }
                }else{
                throw new Exception(tmpsbMsg.toString());
            }
            }else{
            throw new Exception(result[1]);
        }

    }

    /**
     * 获取票据的持有人信息
     * @param billNo
     * @return
     * @throws Exception
     */
    private XElement getBillOwnInfo(String billNo,String billType)throws Exception{
        XmlTextWriter xmlTextWriter = new XmlTextWriter();
        CCB02Header header = new CCB02Header();
        header.REQUEST_SN = getQryTransID();
        header.TX_CODE = "6WH027";
        writeCommonHeader(xmlTextWriter, header);
        xmlTextWriter.writeStartElement("TX_INFO");
//                    7 ECTYPEVALUE          电票类型              varChar(4) F AC01银承,AC02商承

        if ("1".equals(billType)) {
            billType = "AC01";
        } else {
            billType = "AC02";
        }
        xmlTextWriter.writeElementString("ECTYPEVALUE", billType);
//              8 BkTranType 交易类型 varChar(4) F 0001：承兑（在票据类型为：AC02时候，客户可以发起查询）
//              0002：交票/未用退回 0003：退票（只查询出票已经登记的票据）0004：背书 0005：贴现 0006：质押 0007：质押解除
//              0008：提示付款 0009：拒付追索 0010：非拒付追索 0011：追索（包括拒付和非拒付）0012：查验持票查询

        xmlTextWriter.writeElementString("BkTranType", "0008");
//                    9 ECDSNO               汇票号码             varChar(30) T 　
        xmlTextWriter.writeElementString("ECDSNO", billNo);
//                    10 BEGINDATE            票据到期日开始         varChar(8) T YYYYMMDD
        xmlTextWriter.writeElementString("BEGINDATE", "");
//                    11 ENDDATE              票据到期日结束         varChar(8) T YYYYMMDD
        xmlTextWriter.writeElementString("ENDDATE", "");
//                    12 BRCHNO               承兑银行名称 varchar(120) T 　
        xmlTextWriter.writeElementString("BRCHNO", "");
//                    13 BkAmt1 票据金额开始 Number(18,2) T 0或空表示最大值.
        xmlTextWriter.writeElementString("BkAmt1", "");
//                    14 BkAmt2 票据金额结束 Number(18,2) T
        xmlTextWriter.writeElementString("BkAmt2", "");
//                    15 Bk60Name1            出票人名称           varchar(120) T 　
        xmlTextWriter.writeElementString("Bk60Name1", "");
//                    16 Bk8Date3             票据出票日开始       varChar(8) T YYYYMMDD
        xmlTextWriter.writeElementString("Bk8Date3", "");
//                    17 Bk8Date4             票据出票日结束       varChar(8) T YYYYMMDD
        xmlTextWriter.writeElementString("Bk8Date4", "");
//                    18 PAGE                 当前页次             Number(20) T 默认为1
        xmlTextWriter.writeElementString("PAGE",  "1");
//                    19 PAGE_SIZE            每页记录数           Number(20) T 默认为200条记录数
        xmlTextWriter.writeElementString("PAGE_SIZE", "200");
//                    20 BkType4            集团划转标志           Char(1) T 区分上手是否为集团本部标志:0001待转集团 0002 集团划转只有是集团票据才上送以上两个标志
        xmlTextWriter.writeElementString("BkType4", "");
//                    21 BkOthName            收款人名称           varchar(120) T
        xmlTextWriter.writeElementString("BkOthName", "");
        xmlTextWriter.writeEndDocument();
        String[] result = sendToBank(xmlTextWriter.toXmlString());
        if (result[0].length() == 0) {
            StringBuilder tmpsbMsg = new StringBuilder();
            XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1], tmpsbMsg);
            if (tmpbkReader != null) {
                String return_code = tmpbkReader.getFirstText("/TX/RETURN_CODE");
                if (StringHelper.hasAnyChar(return_code) && "000000".equals(return_code)) {
                    String totalNum = tmpbkReader.getFirstText("/TX/TX_INFO/BkTotNum");//总数
//                    String returnNum = tmpbkReader.getFirstText("/TX/TX_INFO/BkNum1");//返回数
                    //记录查询返回总数
                   if("1".equals(totalNum)){
                       tmpbkReader.read("/TX/TX_INFO/DETAILLIST/DETAILINFO");
                       return tmpbkReader.readedXElement;
                    }else {
                       throw new Exception("持票人信息查询失败！");
                   }
                } else {
                    throw new Exception(tmpbkReader.getFirstText("/TX/RETURN_MSG"));
                }
            } else {
                throw new Exception(tmpbkReader.toString());

            }
        } else {
            throw new Exception(result[1]);
        }
    }

    /**
     * 获取票据的应答者信息
     * @param billNo
     * @param billType
     * @return
     * @throws Exception
     */
    private XElement getBillRespInfo(String billNo,String billType,String operater)throws Exception{
        int tmpNowReadStart = 1;//起始页数
        int tmpNowReadNum = 0;//已查询笔数
        int tmpTotalNum = 0;
        do{
        XmlTextWriter xmlTextWriter = new XmlTextWriter();
        CCB02Header header = new CCB02Header();
        header.REQUEST_SN = getQryTransID();
        header.TX_CODE = "6WH028";
        writeCommonHeader(xmlTextWriter, header);
        xmlTextWriter.writeStartElement("TX_INFO");
//                    7 ECTYPEVALUE          电票类型              varChar(4) F AC01银承,AC02商承
        if ("1".equals(billType)) {
            billType = "AC01";
        } else {
            billType = "AC02";
        }
//                8 BkType1 票据类型 Char(4) F AC01银承
//                AC02商承
        xmlTextWriter.writeElementString("BkType1",billType);
//                9 Bk8Date1 交易日期 Char(8) T 　
        xmlTextWriter.writeElementString("Bk8Date1","");
//                10 BkType2 应答类型 Char(4) F 0001其他
//                0002同意清偿
//                0003.交票待签收应答 0004.背书待签收应答
        String busiType="";
        if("1014".equals(operater)){
            busiType = "0002";
        }
        xmlTextWriter.writeElementString("BkType2",busiType);
//                11 __BEGIN_REC 开始记录数 Int F 　
        xmlTextWriter.writeElementString("__BEGIN_REC",tmpNowReadStart+"");
//                12 __MAX_REC 一次查询最大记录数 Int F
        xmlTextWriter.writeElementString("__MAX_REC","200");
        xmlTextWriter.writeEndDocument();
        String[] result = sendToBank(xmlTextWriter.toXmlString());
        if (result[0].length() == 0) {
            StringBuilder tmpsbMsg = new StringBuilder();
            XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1], tmpsbMsg);
            if (tmpbkReader != null) {
                String return_code = tmpbkReader.getFirstText("/TX/RETURN_CODE");
                if ("000000".equals(return_code)) {
                    String totalNum = tmpbkReader.getFirstText("/TX/TX_INFO/BkTotNum");//总数
                    String returnNum = tmpbkReader.getFirstText("/TX/TX_INFO/BkNum1");//返回数
                    //记录查询返回总数
                    if (tmpNowReadNum == 0 && tmpTotalNum == 0) {
                        tmpTotalNum = Integer.valueOf(totalNum);
                    }
                    while (tmpbkReader.read("/TX/TX_INFO/DETAILLIST/DETAILINFO")) {
                        tmpNowReadNum++;
                        tmpNowReadStart++;
                        if(billNo.equals(tmpbkReader.readedXElement.getSingleText("BkListNo1"))){
                            return tmpbkReader.readedXElement;
                        }
                    }
                } else {
                    throw new Exception(tmpbkReader.getFirstText("/TX/RETURN_MSG"));
                }
            } else {
                throw new Exception(tmpbkReader.toString());

            }
        } else {
            throw new Exception(result[1]);
        }
        }while (tmpNowReadNum<tmpTotalNum);
        return null;
    }

    //撤票
    @Override
    public RetAtsB103BillRetSubmit D_B103_BillResp_Submit(ReqB103BillRetSubmit query) throws Exception {
        RetAtsB103BillRetSubmit retAts = new RetAtsB103BillRetSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        query.readRecord();
            RetAtsB103BillRetSubmitDetail detail = new RetAtsB103BillRetSubmitDetail();
            try {
                CCB02Header header = new CCB02Header();
                header.REQUEST_SN =getQryTransID();
                header.TX_CODE = "6WH005";
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                writeCommonHeader(xmlTextWriter,header);
                xmlTextWriter.writeStartElement("TX_INFO");
//                BkListNo1 电子票据号码 varChar(30) F 　 　
                xmlTextWriter.writeElementString("BkListNo1",query.detail.billNo);
//                8 BkElctSgn1 出票人电子签名 varChar(4000 ) F 　 　
                xmlTextWriter.writeElementString("BkElctSgn1",query.detail.billOpenName);
                xmlTextWriter.writeEndElement();
//                9 SIGN_INFO 证书签名 varChar(254) T
                xmlTextWriter.writeElementString("SIGN_INFO","");
                xmlTextWriter.writeElementString("BkDetail1",query.detail.memo+"_"+query.detail.reqSeqID);
                xmlTextWriter.writeEndDocument();
                String [] result = sendToBank(xmlTextWriter.toXmlString());
                if(result[0].length() ==0){
                    StringBuilder tmpsbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1],tmpsbMsg);
                    if(tmpBkReader!=null){
                        String returnCode = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                        if("000000".equals(returnCode)) {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                            detail.reqSeqID =query.detail.reqSeqID;
                            detail.transState = returnCode;
                            detail.payInfoCode = detail.transState;
                        }else {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                            detail.reqSeqID =query.detail.reqSeqID;
                            detail.transState = returnCode;
                            detail.payInfoCode = detail.transState;
                        }
                    }else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpsbMsg.toString();
                    }
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = result[1];
                }
            }catch (Exception ex){
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }
//提示收票
    @Override
    public RetAtsB104BillReceSubmit D_B104_BillResp_Submit(ReqB104BillReceSubmit query) throws Exception {
        RetAtsB104BillReceSubmit retAts = new RetAtsB104BillReceSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        query.readRecord();
            RetAtsB104BillReceSubmitDetail detail = new RetAtsB104BillReceSubmitDetail();
            try {
                CCB02Header header = new CCB02Header();
                header.REQUEST_SN =getQryTransID();
                header.TX_CODE = "6WH004";
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                writeCommonHeader(xmlTextWriter,header);
                xmlTextWriter.writeStartElement("TX_INFO");
//                BkListNo1 电子票据号码 varChar(30) F 　
                xmlTextWriter.writeElementString("BkListNo1",query.detail.billNo);
//                8 BkDetail1 出票人备注 varChar(512 ) T 　
                xmlTextWriter.writeElementString("BkDetail1",query.detail.memo+"_"+query.detail.reqSeqID);

//                9 BkElctSgn1 出票人电子签名 varChar(4000 ) F 　
                xmlTextWriter.writeElementString("BkElctSgn1",query.detail.billOpenName);
                xmlTextWriter.writeEndElement();
//                10 SIGN_INFO 证书签名 varChar(254) T 　
                xmlTextWriter.writeElementString("SIGN_INFO","");
                xmlTextWriter.writeEndDocument();
                String [] result = sendToBank(xmlTextWriter.toXmlString());
                if(result[0].length() ==0){
                    StringBuilder tmpsbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1],tmpsbMsg);
                    if(tmpBkReader!=null){
                        String returnCode = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                        if("000000".equals(returnCode)){
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                            detail.reqSeqID =query.detail.reqSeqID;
                            detail.transState = returnCode;
                            detail.payInfoCode = detail.transState;
                        }else {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                            detail.reqSeqID =query.detail.reqSeqID;
                            detail.transState = returnCode;
                            detail.payInfoCode = detail.transState;
                        }
                    }else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpsbMsg.toString();
                    }
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = result[1];
                }
            }catch (Exception ex){
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }
//背书
    @Override
    public RetAtsB105BillEndoSubmit D_B105_BillResp_Submit(ReqB105BillEndoSubmit query) throws Exception {
        RetAtsB105BillEndoSubmit retAts = new RetAtsB105BillEndoSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        query.readRecord();
            RetAtsB105BillEndoSubmitDetail detail = new RetAtsB105BillEndoSubmitDetail();
            try {
                CCB02Header header = new CCB02Header();
                header.REQUEST_SN =getQryTransID();
                header.TX_CODE = "6WH006";
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                writeCommonHeader(xmlTextWriter,header);
                xmlTextWriter.writeStartElement("TX_INFO");
//                BkListNo1 电子票据号码 varChar(30) F 　
                xmlTextWriter.writeElementString("BkListNo1",query.detail.billNo);
//                8 BkType1 背书不得转让标记 Char(4) F EM00可再转让 EM01不得转让
                String bkType;
                if("1".equals(query.detail.transFlag)){
                    bkType = "EM00";
                }else {
                    bkType = "EM01";
                }
                xmlTextWriter.writeElementString("BkType1",bkType);
//                9 BkDetail1 背书人备注 varChar(512) T 　
                xmlTextWriter.writeElementString("BkDetail1",query.detail.memo+"_"+query.detail.reqSeqID);
//                10 BkRole 背书人类别 Char(4) F 只能为RC01企业
                xmlTextWriter.writeElementString("BkRole","RC01");
//                11 Bk60Name1 背书人名称 varChar(120) F 　
                xmlTextWriter.writeElementString("Bk60Name1",query.detail.endoName);
//                12 BkAcctNo1 背书人账号 varChar(32) F 　
                xmlTextWriter.writeElementString("BkAcctNo1",query.detail.actNum);
//                13 BkBrchNo1 背书人开户行行号 varChar(12) F 　
                xmlTextWriter.writeElementString("BkBrchNo1",query.detail.endoBnkCode);
//                14 Bk60Name3 被背书人名称 varChar(120) F 　
                xmlTextWriter.writeElementString("Bk60Name3",query.detail.receName);
//                15 BkAcctNo3 被背书人账号 varChar(32) F 　
                xmlTextWriter.writeElementString("BkAcctNo3",query.detail.receAct);
//                16 BkBrchNo3 被背书人开户行行号 varChar(12) F 　
                xmlTextWriter.writeElementString("BkBrchNo3",query.detail.receBnkCode);
//                17 BkDetail2 备注2 varChar(255) T 　
                xmlTextWriter.writeElementString("BkDetail2",query.detail.memo);
//                18 BkElctSgn1 背书人电子签名 varChar(4000) F 　
                xmlTextWriter.writeElementString("BkElctSgn1",query.detail.endoName);// TODO: 2018/10/17
                xmlTextWriter.writeEndElement();
//                19 SIGN_INFO 证书签名 varChar(254) T 　
                xmlTextWriter.writeElementString("SIGN_INFO","");
                xmlTextWriter.writeEndDocument();
                String [] result = sendToBank(xmlTextWriter.toXmlString());
                if(result[0].length() ==0){
                    StringBuilder tmpsbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1],tmpsbMsg);
                    if(tmpBkReader!=null){
                        String returnCode = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                        //测试
                        //returnCode= "000000";
                        if(StringHelper.hasAnyChar(returnCode )&&"000000".equals(returnCode)) {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                            detail.reqSeqID =query.detail.reqSeqID;
                            detail.transState = returnCode;
                            detail.payInfoCode = detail.transState;
                        }else {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                            detail.reqSeqID =query.detail.reqSeqID;
                            detail.transState = returnCode;
                            detail.payInfoCode = detail.transState;
                        }
                    }else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpsbMsg.toString();
                    }
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = result[1];
                }
            }catch (Exception ex){
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }
//贴现
    @Override
    public RetAtsB106BillDiscSubmit D_B106_BillResp_Submit(ReqB106BillDiscSubmit query) throws Exception {
        RetAtsB106BillDiscSubmit retAts = new RetAtsB106BillDiscSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        query.readRecord();
        RetAtsB106BillDiscSubmitDetail detail = new RetAtsB106BillDiscSubmitDetail();
            try {
                CCB02Header header = new CCB02Header();
                header.REQUEST_SN =getQryTransID();
                header.TX_CODE = "6WH008";
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                writeCommonHeader(xmlTextWriter,header);
                xmlTextWriter.writeStartElement("TX_INFO");
//                7 BkNum1               贴现总笔数           Number(20) F 　
                xmlTextWriter.writeElementString("BkNum1","1");
//                8 BkAmt9               贴现总金额           Number(18,20) F 　
                xmlTextWriter.writeElementString("BkAmt9",query.detail.billAmt);
//                9 BkElctSgn1           贴现申请人电子签名   varChar(4000) F 　
                xmlTextWriter.writeElementString("BkElctSgn1",query.detail.discName);// TODO: 2018/10/17
//
//                <DETAILLIST>
                xmlTextWriter.writeStartElement("DETAILLIST");
//                <DETAILINFO>
                xmlTextWriter.writeStartElement("DETAILINFO");
//                LIST(多条记录)
//                11 BkListNo1            电子票据号码         varChar(30) F 　
                xmlTextWriter.writeElementString("BkListNo1",query.detail.billNo);
//                12 BKEcdsType           电票类型             CHAR(4) F AC01银承;AC02商承
                String bkEcdsType;
                if("1".equals(query.detail.billType)){
                    bkEcdsType = "AC01";
                }else {
                    bkEcdsType = "AC02";
                }
                xmlTextWriter.writeElementString("BKEcdsType",bkEcdsType);
//                13 Bk60Name1            贴现申请人名称       varChar(120) F 　
                xmlTextWriter.writeElementString("Bk60Name1",query.detail.discName);
//                14 BkAcctNo1            贴现申请人账号       varChar(32) F 　
                xmlTextWriter.writeElementString("BkAcctNo1",query.detail.actNum);
//                15 BkBrchNo1            贴现申请人开户行行号 varChar(12) F 　
                xmlTextWriter.writeElementString("BkBrchNo1",query.detail.discBnkCode);
//                18 BkRate1              贴现利率             Number(7,6) F 　单位是%
                xmlTextWriter.writeElementString("BkRate1",query.detail.discRate);
//                20 BkType1              贴现种类             CHAR(4) F 只能为RM00买断式;
                String bkType;
                if("1".equals(query.detail.discType)){
                    bkType = "RM00";
                }else {
                    throw new Exception("银行只支持买断式贴现!");
                }
                xmlTextWriter.writeElementString("BkType1",bkType);
                if("1".equals(query.detail.discType)) {
//                    19 BkRate2              贴现赎回利率         Number(7,6) T 如为回购式，则必填写
                    xmlTextWriter.writeElementString("BkRate2", "");
//                    23 Bk8Date2             贴现赎回开放日       varChar(8) T 贴现种类“回购式”时为必填
                    xmlTextWriter.writeElementString("Bk8Date2", "");
//                    24 Bk8Date3             贴现赎回截止日       varChar(8) T 贴现种类“回购式”时为必填
                    xmlTextWriter.writeElementString("Bk8Date3", "");
//                    25 BkAmt3               贴现赎回金额         Number(18,2) T 如为回购式，则必填写
                    xmlTextWriter.writeElementString("BkAmt3", "");
                }else{
//                    19 BkRate2              贴现赎回利率         Number(7,6) T 如为回购式，则必填写
                    xmlTextWriter.writeElementString("BkRate2", query.detail.repRate);
//                    23 Bk8Date2             贴现赎回开放日       varChar(8) T 贴现种类“回购式”时为必填
                    xmlTextWriter.writeElementString("Bk8Date2", DateHelper.convertD10toD8(query.detail.repOpenDate));
//                    24 Bk8Date3             贴现赎回截止日       varChar(8) T 贴现种类“回购式”时为必填
                    xmlTextWriter.writeElementString("Bk8Date3", DateHelper.convertD10toD8(query.detail.repDueDate));
//                    25 BkAmt3               贴现赎回金额         Number(18,2) T 如为回购式，则必填写
                    xmlTextWriter.writeElementString("BkAmt3", query.detail.repAmt);
                }
                BigDecimal discAmt = getDiscAmt(query.detail);
//                21 BkAmt2               贴现实付金额         Number(18,2) F 　
                xmlTextWriter.writeElementString("BkAmt2", discAmt.setScale(2,BigDecimal.ROUND_HALF_UP).toString());
//                26 BkListNo2            贴现交易合同编号     varChar(30) T 　
                xmlTextWriter.writeElementString("BkListNo2","");
//                27 BkListNo3            贴现发票号码         varChar(30) T 　
                xmlTextWriter.writeElementString("BkListNo3","");
//                28 Bk60Name3            贴入人名称           varChar(120) F 　
                xmlTextWriter.writeElementString("Bk60Name3",query.detail.receName);
//                30 BkBrchNo3            贴入人开户行行号     varChar(12) F 　
                xmlTextWriter.writeElementString("BkBrchNo3",query.detail.receBnkCode);
//                31 BkAcctNo4            入账账号             varChar(32) F 　
                xmlTextWriter.writeElementString("BkAcctNo4",query.detail.actedAct);
//                32 BkBrchNo4            入账开户行号           varChar(32) F 　
                xmlTextWriter.writeElementString("BkBrchNo4",query.detail.actedBnkCode);
                 if("2".equals(query.detail.payInterestType))
                {
//                33 Bk60Name4            付息人名称           varChar(120) T 付息方式：2、3时候填写
                    xmlTextWriter.writeElementString("Bk60Name4", query.detail.payInterestName);
//                34 BkAcctNo5            付息人账号           varChar(32) T 付息方式：2、3时候填写
                    xmlTextWriter.writeElementString("BkAcctNo5", query.detail.payInterestAct);
//                    35 BkRate               付息比例             Number(7,6) T 付息方式：3时候填写
                    xmlTextWriter.writeElementString("BkRate", "");
                }else if("3".equals(query.detail.payInterestType)){
                    xmlTextWriter.writeElementString("Bk60Name4",query.detail.payInterestName);
                    xmlTextWriter.writeElementString("BkAcctNo5",query.detail.payInterestAct);
                    xmlTextWriter.writeElementString("BkRate",query.detail.payInterestRate);
                }else {
                     xmlTextWriter.writeElementString("Bk60Name4", "");
                     xmlTextWriter.writeElementString("BkAcctNo5", "");
                     xmlTextWriter.writeElementString("BkRate", "");
                 }
//                36 BkFlag3              付息方式             CHAR(1) F 1：卖方付息;2：买方付息;3：协议付息
                xmlTextWriter.writeElementString("BkFlag3",query.detail.payInterestType);
//                37 BkEcdBatchNo         批次号               varChar(10) T 　
                xmlTextWriter.writeElementString("BkEcdBatchNo","");
//                40 BkDetail1            贴出人备注           varChar(512) T 　
                xmlTextWriter.writeElementString("BkDetail1",query.detail.memo+"_"+query.detail.reqSeqID);
//                41 BkDetail2            备注2                varChar(20) T 　
                xmlTextWriter.writeElementString("BkDetail2","");
                xmlTextWriter.writeEndElement();
                xmlTextWriter.writeEndElement();
//                10 SIGN_INFO 证书签名 varChar(254) T 　 　
                xmlTextWriter.writeElementString("SIGN_INFO","");
                xmlTextWriter.writeEndDocument();
                String [] result = sendToBank(xmlTextWriter.toXmlString());
                if(result[0].length() ==0){
                    StringBuilder tmpsbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1],tmpsbMsg);
                    if(tmpBkReader!=null){
                        String returnCode = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                        if(StringHelper.hasAnyChar(returnCode )&&"000000".equals(returnCode)){
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/TX/TX_INFO/DETAILLIST/DETAILINFO/BkDetail1");
                            detail.reqSeqID =query.detail.reqSeqID;
                            detail.transState = tmpBkReader.getFirstText("/TX/TX_INFO/DETAILLIST/DETAILINFO/BkFlag1");
                            detail.payInfoCode = detail.transState;
                        }else {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                            detail.reqSeqID =query.detail.reqSeqID;
                            detail.transState = returnCode;
                            detail.payInfoCode = detail.transState;
                        }
                    }else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpsbMsg.toString();
                    }
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = result[1];
                }
            }catch (Exception ex){
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }
//质押
    @Override
    public RetAtsB107BillPledSubmit D_B107_BillResp_Submit(ReqB107BillPledSubmit query) throws Exception {
//        6WH009
        RetAtsB107BillPledSubmit retAts = new RetAtsB107BillPledSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
         query.readRecord();
        RetAtsB107BillPledSubmitDetail detail = new RetAtsB107BillPledSubmitDetail();
            try {
                CCB02Header header = new CCB02Header();
                header.REQUEST_SN =getQryTransID();
                header.TX_CODE = "6WH009";
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                writeCommonHeader(xmlTextWriter,header);
                xmlTextWriter.writeStartElement("TX_INFO");
//                BkListNo1            电子票据号码         varChar(30) F 　
//                8 ecdsType             电票类型             CHAR(4) F AC01 银承 ; AC02商承
                xmlTextWriter.writeElementString("BkListNo1",query.detail.billNo);
                String ecdsType;
                if("1".equals(query.detail.billType)){
                    ecdsType = "AC01";
                }else {
                    ecdsType = "AC02";
                }
                xmlTextWriter.writeElementString("ecdsType",ecdsType);
//                10 BkAcctNo1            出质人账号           varChar(32) F 如发起行为贴出行，填写一个‘0’；其他情况下，填写贴出人账号
                xmlTextWriter.writeElementString("BkAcctNo1",query.detail.actNum);
//                11 Bk60Name1            出质人名称           varChar(120) F 　
                xmlTextWriter.writeElementString("Bk60Name1",query.detail.pledName);
//                12 BkBrchNo1            出质人开户行行号     varChar(12) F 　
                xmlTextWriter.writeElementString("BkBrchNo1",query.detail.pledBnkCode);
//                15 BkDetail1            出质人备注             varChar(512) T 　
                xmlTextWriter.writeElementString("BkDetail1",query.detail.memo+"_"+query.detail.reqSeqID);
//                19 BkEcdBatchNo         批次号               varChar(10) T 　
                xmlTextWriter.writeElementString("BkEcdBatchNo","");
//                20 Bk60Name3            质权人名称           varChar(120) F 　
                xmlTextWriter.writeElementString("Bk60Name3",query.detail.receName);
//                21 BkAcctNo3            质权人账号           varChar(32) F 如果质押给银行，账号必须填‘0’
                xmlTextWriter.writeElementString("BkAcctNo3",query.detail.receAct);
//                22 BkBrchNo3            质权人开户行行号     varChar(12) F 　
                xmlTextWriter.writeElementString("BkBrchNo3",query.detail.receBnkCode);
//                26 BkHvpBrchNo          大额支付行号         varChar(12) F 大额支付行号
                xmlTextWriter.writeElementString("BkHvpBrchNo",query.detail.receBnkCode);
//                27 BkElctSgn1           出质人电子签名       varChar(4000) F 　
                xmlTextWriter.writeElementString("BkElctSgn1",query.detail.pledName);
                xmlTextWriter.writeEndElement();
//                28 SIGN_INFO 证书签名 varChar(254) T 　
                xmlTextWriter.writeElementString("SIGN_INFO","");
                xmlTextWriter.writeEndDocument();
                String [] result = sendToBank(xmlTextWriter.toXmlString());
                if(result[0].length() ==0){
                    StringBuilder tmpsbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1],tmpsbMsg);
                    if(tmpBkReader!=null){
                        String returnCode = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                        if("000000".equals(returnCode)) {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                            detail.reqSeqID =query.detail.reqSeqID;
                            detail.transState = returnCode;
                            detail.payInfoCode = detail.transState;
                        }else {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                            detail.reqSeqID =query.detail.reqSeqID;
                            detail.transState = returnCode;
                            detail.payInfoCode = detail.transState;
                        }
                    }else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpsbMsg.toString();
                    }
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = result[1];
                }
            }catch (Exception ex){
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }
//提示付款
    @Override
    public RetAtsB108BillPaySubmit D_B108_BillResp_Submit(ReqB108BillPaySubmit query) throws Exception {
//        提示付款	6WH011	提示付款
        RetAtsB108BillPaySubmit retAts = new RetAtsB108BillPaySubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        query.readRecord();
        RetAtsB108BillPaySubmitDetail detail = new RetAtsB108BillPaySubmitDetail();
        try {
            XElement element = getBillOwnInfo(query.detail.billNo, query.detail.billNo.substring(0, 1));
            CCB02Header header = new CCB02Header();
            header.REQUEST_SN = getQryTransID();
            header.TX_CODE = "6WH011";
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            writeCommonHeader(xmlTextWriter, header);
            xmlTextWriter.writeStartElement("TX_INFO");
//                7 BkNum1 提示付款总笔数 Int F 　
            xmlTextWriter.writeElementString("BkNum1", "1");
//                8 BkAmt9 提示付款总金额 Decimal(18,2) F 　
            xmlTextWriter.writeElementString("BkAmt9", query.detail.billAmt);
//                9 SIGN_INFO 证书签名 varChar(254) T 　
            xmlTextWriter.writeElementString("SIGN_INFO", "");
//                10 BkListNo1 电子票据号码 Char(30) F 　
            xmlTextWriter.writeElementString("BkListNo1", query.detail.billNo);
//                11 BkAmt2 提示付款金额 Decimal(18,2) F 系统应判断等于票据金额
            xmlTextWriter.writeElementString("BkAmt2", query.detail.billAmt);
//                12 Bk8Date2 提示付款申请日期 Char(8) F 预约必输
            xmlTextWriter.writeElementString("Bk8Date2", Format.Date8Format.format(new Date()));
//                ECDS自动发起提示付款日期
//                13 BkDetail2 逾期原因说明 varChar(512) T 超过票据到期10天后委托收款的，网银系统提示客户输入逾期托收理由（内容不限）
            xmlTextWriter.writeElementString("BkDetail2", query.detail.overDueRea);
//                14 BkFlag3 清算方式 Char(1) T 商承使用：0：自动清算 1：手工清算
            xmlTextWriter.writeElementString("BkFlag3", "1".equals(query.detail.payType) ? "0" : "1");// TODO: 2018/10/17
//                15 BkDetail1 提示付款人备注 varChar(512) T 　
            xmlTextWriter.writeElementString("BkDetail1", query.detail.memo + "_" + query.detail.reqSeqID);
//                16 BkRole 提示付款人类别 Char(4) F 银行
            String bkRole = getBankConfig().getMapValueByKey("bkRole", query.detail.actNum);
            xmlTextWriter.writeElementString("BkRole", bkRole);
//                17 Bk60Name1 提示付款人名称 varChar(120) F 　
            xmlTextWriter.writeElementString("Bk60Name1", element.getSingleText("Bk60Name1"));
//                18 BkAcctNo1 提示付款人账号 varChar(32) F 如发起行为提示付款行，填写一个‘0’；如为人民银行的，也填写一个‘0’；其他情况下，填写提示付款人账号
            xmlTextWriter.writeElementString("BkAcctNo1", element.getSingleText("BkAcctNo1"));
//                19 BkBrchNo1 提示付款人开户行行号 varChar(12) F 　
            xmlTextWriter.writeElementString("BkBrchNo1", element.getSingleText("BkBrchNo1"));
//                20 BkBrchNo2 提示付款人承接行行号 varChar(12) T 　
            xmlTextWriter.writeElementString("BkBrchNo2", "");
//                21 BkElctSgn1 提示付款人电子签名 varChar(4000) F 　
            xmlTextWriter.writeEndElement();
            xmlTextWriter.writeElementString("BkElctSgn1", element.getSingleText("Bk60Name1"));// TODO: 2018/10/17
            xmlTextWriter.writeEndDocument();
            String[] result = sendToBank(xmlTextWriter.toXmlString());
            if (result[0].length() == 0) {
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1], tmpsbMsg);
                if (tmpBkReader != null) {
                    String returnCode = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                    if ( "000000".equals(returnCode)) {
                        detail.respCode = "0";
                        detail.respInfo = tmpBkReader.getFirstText("/TX/TX_INFO/DETAILLIST/DETAILINFO/BkDetail1");
                        detail.reqSeqID = query.detail.reqSeqID;
                        detail.transState = tmpBkReader.getFirstText("/TX/TX_INFO/DETAILLIST/DETAILINFO/BkFlag1");
                        detail.payInfoCode = detail.transState;
                    } else {
                        detail.respCode = "0";
                        detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                        detail.reqSeqID =query.detail.reqSeqID;
                        detail.transState = returnCode;
                        detail.payInfoCode = detail.transState;
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpsbMsg.toString();
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = result[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }
//提示保证
    @Override
    public RetAtsB109BillGuarSubmit D_B109_BillResp_Submit(ReqB109BillGuarSubmit query) throws Exception {
//        提示保证	6WH007	提示保证
//        RetAtsB109BillGuarSubmit retAts = new RetAtsB109BillGuarSubmit(query.reqHeader);
//        retAts.writeBeforeDetail();
//        while (query.readRecord()){
//            RetAtsB109BillGuarSubmitDetail detail = new RetAtsB109BillGuarSubmitDetail();
//            try {
//                CCB02Header header = new CCB02Header();
//                header.REQUEST_SN =getQryTransID();
//                header.TX_CODE = "6WH007";
//                XmlTextWriter xmlTextWriter = new XmlTextWriter();
//                writeCommonHeader(xmlTextWriter,header);
//                xmlTextWriter.writeStartElement("TX_INFO");
////                7 BkType1 保证类别 Char(4) F 0：出票保证
////                1：承兑保证
////                2：背书保证
//                String bkType;
//                if("1".equals(query.detail.guarType)){
//                    bkType ="1";
//                }else if("2".equals(query.detail.guarType)){
//                    bkType = "2";
//                }else{
//                    bkType = "3";
//                }
//                xmlTextWriter.writeElementString("BkType1",bkType);
////                8 BkListNo1 电子票据号码 varChar(30) F 　
//                xmlTextWriter.writeElementString("BkListNo1",query.detail.billNo);
////                9 BkDetail1 被保证人备注 varChar(512) T 　
//                xmlTextWriter.writeElementString("BkDetail1",query.detail.memo);
////                10 BkRole 被保证人类别 Char(4) F 只能为RC01企业
//                xmlTextWriter.writeElementString("BkRole","RC01");
////                11 Bk60Name1 被保证人名称 varChar(120) F 　
//                xmlTextWriter.writeElementString("Bk60Name1",query.detail.guarName);
////                12 BkAcctNo1 被保证人账号 varChar(32) F 　
//                xmlTextWriter.writeElementString("BkAcctNo1",query.detail.actNum);
////                13 BkBrchNo1 被保证人开户行行号 varChar(12) F 　
//                xmlTextWriter.writeElementString("BkBrchNo1","");//// TODO: 2018/10/17
////                14 Bk60Name2 保证人名称 varChar(120) F 　
//                xmlTextWriter.writeElementString("Bk60Name2",query.detail.receName);
////                15 BkAcctNo2 保证人账号 varChar(32) F 　
//                xmlTextWriter.writeElementString("BkAcctNo2",query.detail.receAct);
////                16 BkBrchNo2 保证人开户行行号 varChar(12) F 　
//                xmlTextWriter.writeElementString("BkBrchNo2",query.detail.receBnkCode);
////                17 BkEcdBatchNo 批次号 varChar(10) T 　
//                xmlTextWriter.writeElementString("BkEcdBatchNo","");
////                18 BkElctSgn1 电子签名 varChar(4000) F 　
//                xmlTextWriter.writeElementString("BkElctSgn1","");
////                19 SIGN_INFO 证书签名 varChar(254) T 　
//                xmlTextWriter.writeElementString("SIGN_INFO","");
//                String [] result = sendToBank(xmlTextWriter.toXmlString());
//                if(result[0].length() ==0){
//                    StringBuilder tmpsbMsg = new StringBuilder();
//                    XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1],tmpsbMsg);
//                    if(tmpBkReader!=null){
//                        String returnCode = tmpBkReader.getFirstText("/TX/RETURN_CODE");
//                        //测试
//                        //returnCode= "000000";
//                        if(StringHelper.hasAnyChar(returnCode )&&"000000".equals(returnCode)) {
//                            detail.respCode = "0";
//                            detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
//                            detail.reqSeqID =query.detail.reqSeqID;
//                            detail.transState = returnCode;
//                            detail.payInfoCode = detail.transState;
//                        }else {
//                            detail.respCode = "-1";
//                            detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
//                        }
//                    }else {
//                        detail.respCode = "-1";
//                        detail.respInfo = tmpsbMsg.toString();
//                    }
//                }else{
//                    detail.respCode = "-1";
//                    detail.respInfo = result[1];
//                }
//            }catch (Exception ex){
//                ex.printStackTrace();
//                detail.respCode = "-1";
//                detail.respInfo = ex.getMessage();
//            }
//            retAts.writeDetail(detail);
//        }
//        retAts.writeEnd();
//        return retAts;
        RetAtsB109BillGuarSubmit retAts = new RetAtsB109BillGuarSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        while (query.readRecord()){
            RetAtsB109BillGuarSubmitDetail detail = new RetAtsB109BillGuarSubmitDetail();
            try {
                CCB02Header header = new CCB02Header();
                header.REQUEST_SN =getQryTransID();
                header.TX_CODE = "6WH010";
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                writeCommonHeader(xmlTextWriter,header);
                xmlTextWriter.writeStartElement("TX_INFO");
                XElement billInfo = getBillInfoByOwn( query.detail.actNum,"2",query.detail.billNo,"0007");
//                7 BkListNo1            电子票据号码         varChar(30) F 　
                xmlTextWriter.writeElementString("BkListNo1",billInfo.getSingleText("BkListNo1"));
//                8 ecdsType             电票类型             varChar(4) F AC01:银承；AC02:商承
                xmlTextWriter.writeElementString("ecdsType",billInfo.getSingleText("BkType1"));
//                10 Bk60Name1            质权人名称           varChar(120) F 　
                xmlTextWriter.writeElementString("Bk60Name1",billInfo.getSingleText("Bk60Name1"));
//                11 BkAcctNo1            质权人账号          varChar(32) F 必须与原质押报文中的质权人一致
                xmlTextWriter.writeElementString("BkAcctNo1",billInfo.getSingleText("BkAcctNo1"));
//                12 BkBrchNo1            质权人开户行行号     varChar(12) F 必须与原质押报文中的质权人一致
                xmlTextWriter.writeElementString("BkBrchNo1",billInfo.getSingleText("BkBrchNo1"));
//
//                15 BkDetail1            质权人备注           varChar(512) T 　
                xmlTextWriter.writeElementString("BkDetail1",query.detail.reqSeqID);
//                20 BkHvpBrchNo          大小额行号 (联行号)          varChar(12) F 大额行号
                xmlTextWriter.writeElementString("BkHvpBrchNo",billInfo.getSingleText("BkBrchNo4"));
//                24 BkEcdBatchNo         批次号               varChar(10) T 　
                xmlTextWriter.writeElementString("BkEcdBatchNo","");
//                28 BkElctSgn1           质权人电子签名       varChar(4000) F 　
                xmlTextWriter.writeElementString("BkElctSgn1",billInfo.getSingleText("Bk60Name1"));
// TODO: 2018/10/17
                xmlTextWriter.writeEndElement();
//                28 SIGN_INFO 证书签名 varChar(254) T 　
                xmlTextWriter.writeElementString("SIGN_INFO","");
                xmlTextWriter.writeEndDocument();

                String [] result = sendToBank(xmlTextWriter.toXmlString());
                if(result[0].length() ==0){
                    StringBuilder tmpsbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1],tmpsbMsg);
                    if(tmpBkReader!=null){
                        String returnCode = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                        //returnCode= "000000";
                        if(StringHelper.hasAnyChar(returnCode )&&"000000".equals(returnCode)) {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                            detail.reqSeqID =query.detail.reqSeqID;
                            detail.transState = returnCode;
                            detail.payInfoCode = detail.transState;
                        }else {
                            detail.respCode = "-1";
                            detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                        }
                    }else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpsbMsg.toString();
                    }
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = result[1];
                }
            }catch (Exception ex){
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }



    //追索通知
    @Override
    public RetAtsB110BillRecoSubmit D_B110_BillResp_Submit(ReqB110BillRecoSubmit query) throws Exception {
//        追索通知	6WH012	追索通知
        RetAtsB110BillRecoSubmit retAts = new RetAtsB110BillRecoSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        query.readRecord();
        RetAtsB110BillRecoSubmitDetail detail = new RetAtsB110BillRecoSubmitDetail();
        try {
            CCB02Header header = new CCB02Header();
            header.REQUEST_SN = getQryTransID();
            header.TX_CODE = "6WH012";
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            writeCommonHeader(xmlTextWriter, header);
            xmlTextWriter.writeStartElement("TX_INFO");
//                BkListNo1 电子票据号码      varChar(30) F 　
            xmlTextWriter.writeElementString("BkListNo1", query.detail.billNo);
//                2 BkAmt1 票据金额          Decimal(18,2) F 　
            xmlTextWriter.writeElementString("BkAmt1", query.detail.billAmt);
//                3 BkType1 追索类型          varChar(4) F RT00拒付追索  RT01非拒付追索
            String recourseType = query.detail.recourseType;
            if ("1".equals(recourseType)) {
                xmlTextWriter.writeElementString("BkType1", "RT00");
            } else {
                xmlTextWriter.writeElementString("BkType1", "RT01");
            }
//                4 BkAmt2  追索金额          Decimal(18,2) F 不做检查
            xmlTextWriter.writeElementString("BkAmt2", query.detail.recourseAmt);
//                5 BkFlag1 追索理由代码 varChar(4) T 非拒付追索时必填 RC00 承兑人被依法宣告破产   RC01 承兑人因违法被责令终止活动
            if ("2".equals(recourseType)) {
                if ("1".equals(query.detail.recourseCode)) {
                    xmlTextWriter.writeElementString("BkFlag1", "RC00");
                } else if ("2".equals(query.detail.recourseCode)) {
                    xmlTextWriter.writeElementString("BkFlag1", "RC01");
                }
            }
//                6 BkSumry  追索通知备注      varChar(512) T 　
            xmlTextWriter.writeElementString("BkSumry", query.detail.memo + "_" + query.detail.reqSeqID);
//                7 Bk60Name1 追索人名称        varChar(120) F 　
            xmlTextWriter.writeElementString("Bk60Name1", query.detail.recoName);
//                8 BkElctSgn1 追索人电子签名    varChar(2000) F 　
            xmlTextWriter.writeElementString("BkElctSgn1", query.detail.recoName);
//                9 BkAcctNo1 追索人账号        varChar(32) F 　
            xmlTextWriter.writeElementString("BkAcctNo1", query.detail.actNum);
//                10 BkBrchNo1 追索人开户行行号  varChar(12) F 　
            xmlTextWriter.writeElementString("BkBrchNo1", query.detail.recoBnkCode);
//                11 BkBrchNo2 追索人承接行行号  varChar(12) T 　
            xmlTextWriter.writeElementString("BkBrchNo2", "");
//                Detail（多条信息）
//                DETAILLIST>
            xmlTextWriter.writeStartElement("DETAILLIST");
//                <DETAILINFO>
            xmlTextWriter.writeStartElement("DETAILINFO");
//                13 BkInstNo1 被追索人组织机构代码 varChar(20) F 　 　
            xmlTextWriter.writeElementString("BkInstNo1", query.detail.receCode);
//                14 Bk60Name3 被追索人名称        varChar(120) F 　 　
            xmlTextWriter.writeElementString("Bk60Name3", query.detail.receName);
//                15 BkAcctNo3 被追索人账号        varChar(32) F 　 　
            xmlTextWriter.writeElementString("BkAcctNo3", query.detail.receAct);
//                16 BkBrchNo3 追索人开户行行号    varChar(12) F 　 　
            xmlTextWriter.writeElementString("BkBrchNo3", query.detail.receBnkCode);
//                17 BkBrchNo4 被追索人承接行行号 varChar(12) F 　
            xmlTextWriter.writeElementString("BkBrchNo4", query.detail.receBnkCode);
            xmlTextWriter.writeEndElement();
            xmlTextWriter.writeEndElement();
            xmlTextWriter.writeEndElement();
//                12 SIGN_INFO 签名信息 varChar(254) T 　
            xmlTextWriter.writeElementString("SIGN_INFO", "");
            xmlTextWriter.writeEndDocument();
            String[] result = sendToBank(xmlTextWriter.toXmlString());
            if (result[0].length() == 0) {
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1], tmpsbMsg);
                if (tmpBkReader != null) {
                    String returnCode = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                    if ("000000".equals(returnCode)) {
                        detail.respCode = "0";
                        detail.respInfo = tmpBkReader.getFirstText("/TX/TX_INFO/DETAILLIST/DETAILINFO/BkDetail1");
                        detail.reqSeqID = query.detail.reqSeqID;
                        detail.transState = tmpBkReader.getFirstText("/TX/TX_INFO/DETAILLIST/DETAILINFO/BkFlag1");
                        detail.payInfoCode = detail.transState;
                    } else {
                        detail.respCode = "0";
                        detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                        detail.reqSeqID =query.detail.reqSeqID;
                        detail.transState = returnCode;
                        detail.payInfoCode = detail.transState;
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpsbMsg.toString();
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = result[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }
//同意清偿
    @Override
    public RetAtsB111BillAgreeSubmit D_B111_BillResp_Submit(ReqB111BillAgreeSubmit query) throws Exception {
//        同意清偿	6WH013	同意清偿
        RetAtsB111BillAgreeSubmit retAts = new RetAtsB111BillAgreeSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        query.readRecord();
            RetAtsB111BillAgreeSubmitDetail detail = new RetAtsB111BillAgreeSubmitDetail();
            try {
                XElement respinfo = getBillRespInfo(query.detail.billNo,query.detail.billNo.substring(0,1),"1014");
                if(respinfo==null){
                    throw new Exception("获取同意清偿人信息失败");
                }
                CCB02Header header = new CCB02Header();
                header.REQUEST_SN =getQryTransID();
                header.TX_CODE = "6WH013";
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                writeCommonHeader(xmlTextWriter,header);
                xmlTextWriter.writeStartElement("TX_INFO");
//                BkListNo1      电子票据号码               varChar(30) F　 　
                xmlTextWriter.writeElementString("BkListNo1",query.detail.billNo);
//                2 BkAmt1         票据金额                    Decimal(18,2) F 　 　
                xmlTextWriter.writeElementString("BkAmt1",query.detail.billAmt);
//                3 Bk8Date1       同意清偿日期                Char(8) F YYYYMMDD
                xmlTextWriter.writeElementString("Bk8Date1",Format.Date8Format.format(new Date()));
//                4 BkAmt2         同意清偿金额                Decimal(18,2) F 　 　
                xmlTextWriter.writeElementString("BkAmt2",query.detail.agreeAmt);
//                5 BkDetail2      同意清偿人备注            varChar(512) T 　 　
                xmlTextWriter.writeElementString("BkDetail2",query.detail.memo+"_"+query.detail.reqSeqID);
//                6 Bk60Name1      同意清偿人信息名称        varChar(120) F 　 　
                xmlTextWriter.writeElementString("Bk60Name1",respinfo.getSingleText("Bk60Name7"));
//                7 BkElctSgn1     电子签名 varChar(2000) F 　
                xmlTextWriter.writeElementString("BkElctSgn1","");
//                8 BkAcctNo1      同意清偿人信息账号       varChar(32) F 　 　
                xmlTextWriter.writeElementString("BkAcctNo1",query.detail.actNum);
//                9 BkBrchNo1      同意清偿人信息开户行行号   varChar(12) F 　 　
                xmlTextWriter.writeElementString("BkBrchNo1",respinfo.getSingleText("BkBrchNo7"));
//                10 BkBrchNo2      同意清偿人承接行行号    varChar(12) T 　 　
                xmlTextWriter.writeElementString("BkBrchNo2","");
                String[] ecdsInfo = query.detail.busiNo.split("_");
//                11 BkOldPlatDate  原ECDS平台日期          Char(8) F 　 　
                xmlTextWriter.writeElementString("BkOldPlatDate",ecdsInfo[0]);
//                12 BkOldSeq  原ECDS平台流水            NUMBER(20) F 　
                xmlTextWriter.writeElementString("BkOldSeq",ecdsInfo[1]);
                xmlTextWriter.writeEndElement();
//                28 SIGN_INFO 证书签名 varChar(254) T 　
                xmlTextWriter.writeElementString("SIGN_INFO","");
                xmlTextWriter.writeEndDocument();
                String [] result = sendToBank(xmlTextWriter.toXmlString());
                if(result[0].length() ==0){
                    StringBuilder tmpsbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1],tmpsbMsg);
                    if(tmpBkReader!=null){
                        String returnCode = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                        if("000000".equals(returnCode)) {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                            detail.reqSeqID =query.detail.reqSeqID;
                            detail.transState = returnCode;
                            detail.payInfoCode = detail.transState;
                        }else {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                            detail.reqSeqID =query.detail.reqSeqID;
                            detail.transState = returnCode;
                            detail.payInfoCode = detail.transState;
                        }
                    }else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpsbMsg.toString();
                    }
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = result[1];
                }
            }catch (Exception ex){
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }
//交易撤回
    @Override
    public RetAtsB112BillCancSubmit D_B112_BillResp_Submit(ReqB112BillCancSubmit query) throws Exception {
//        交易撤回	6WH015	撤销交易请求

        RetAtsB112BillCancSubmit retAts = new RetAtsB112BillCancSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        while (query.readRecord()){
            RetAtsB112BillCancSubmitDetail detail = new RetAtsB112BillCancSubmitDetail();
            try {
                CCB02Header header = new CCB02Header();
                header.REQUEST_SN =getQryTransID();
                header.TX_CODE = "6WH015";
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                writeCommonHeader(xmlTextWriter,header);
                xmlTextWriter.writeStartElement("TX_INFO");
                //    撤回类型 1-承兑申请撤回 2-提示收票申请撤回 3-背书转让申请撤回 4-贴现申请撤回 5-质押申请撤回 6-（逾期）提示付款撤回
//    7-出票保证申请撤回 8-承兑保证申请撤回  9-背书保证申请撤回 10-追索通知撤回 11-同意清偿撤回 12-解质押申请撤回
                String bkType;
                switch (Integer.valueOf(query.detail.cancelType)){
                    case 1:bkType ="0001";break;//
                    case 2:bkType ="0002";break;
                    case 3:bkType ="0003";break;
                    case 4:bkType ="0004";break;
                    case 5:bkType ="0005";break;
                    case 6:bkType ="0007";break;
                    case 7:bkType ="0016";break;
                    case 8:bkType ="0017";break;
                    case 9:bkType ="0018";break;
                    case 10:bkType ="0008";break;
                    case 11:bkType ="0009";break;
                    case 12:bkType ="0006";break;
                    default:throw new Exception("未知的撤销类型："+query.detail.cancelType);
                }
                xmlTextWriter.writeElementString("BkType1",bkType);
//                8 BkListNo1 电子票据号码 Char(30) F 　
                xmlTextWriter.writeElementString("BkListNo1",query.detail.billNo);
//                9 BkRole 撤销申请人类别 Char(4) F RC00:接入行行
//                RC01:企业
//                RC02:人民银行
//                RC03:被代理行
//                RC04:被代理财务公司
//                RC05:接入财务公司
                String bkrole = getBankConfig().getMapValueByKey("cancelRole",query.detail.actNum);
                xmlTextWriter.writeElementString("BkRole",bkrole);
//                10 BkAcctNo1 撤销申请人账号 varChar(32) F 银行发起的话填“0”否则填发起人账号
                xmlTextWriter.writeElementString("BkAcctNo1",query.detail.actNum);
//                11 BkBrchNo1 撤销申请人开户行行号 varChar(12) F 大额支付行号
                xmlTextWriter.writeElementString("BkBrchNo1",query.detail.cancBnkCode);
//                12 BkBrchNo2 撤回申请人承接行行号 varChar(12) T 如果不存在，则不必填写
                xmlTextWriter.writeElementString("BkBrchNo2","");
                String[] ecdsInfo = query.detail.busiNo.split("_");
//                13 BkOldPlatDate 原ECDS平台日期 Char(8) F 　
                xmlTextWriter.writeElementString("BkOldPlatDate",ecdsInfo[0]);
//                14 BkOldSeq 原ECDS平台流水号 Char(8) F 　
                xmlTextWriter.writeElementString("BkOldSeq",ecdsInfo[1]);
//                15 BkElctSgn1 撤销申请人电子签名 varChar(4000) F 网银方面必填
                xmlTextWriter.writeElementString("BkElctSgn1","");//// TODO: 2018/10/17
                xmlTextWriter.writeEndElement();
//                28 SIGN_INFO 证书签名 varChar(254) T 　
                xmlTextWriter.writeElementString("SIGN_INFO","");
                xmlTextWriter.writeElementString("BkDetail1",query.detail.memo+"_"+query.detail.reqSeqID);
                xmlTextWriter.writeEndDocument();
                String [] result = sendToBank(xmlTextWriter.toXmlString());
                if(result[0].length() ==0){
                    StringBuilder tmpsbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1],tmpsbMsg);
                    if(tmpBkReader!=null){
                        String returnCode = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                        if("000000".equals(returnCode)) {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                            detail.reqSeqID =query.detail.reqSeqID;
                            detail.transState = returnCode;
                            detail.payInfoCode = detail.transState;
                        }else {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                            detail.reqSeqID =query.detail.reqSeqID;
                            detail.transState = returnCode;
                            detail.payInfoCode = detail.transState;
                        }
                    }else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpsbMsg.toString();
                    }
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = result[1];
                }
            }catch (Exception ex){
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB113ProtocolSubmit D_B113_ProtocolResp_Submit(ReqB113ProtocolSubmit query) throws Exception {
        RetAtsB113ProtocolSubmit retAts = new RetAtsB113ProtocolSubmit(query.reqHeader);
        retAts.writeStart();
        RetAtsB113ProtocolSubmitDetail detail = new RetAtsB113ProtocolSubmitDetail();
        try {
            CCB02Header header = new CCB02Header();
            header.REQUEST_SN =getQryTransID();
            header.TX_CODE = "6WH002";
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            writeCommonHeader(xmlTextWriter,header);
            xmlTextWriter.writeStartElement("TX_INFO");
//            BkListNo1            协议编号             varChar(40) F 协议编号（即CLPM合同编号）下可能存在多笔待出票的电票信息，网银一次提交。我行ECDS会进行逐一出票处理
//            XmlTextReader xmlTextReader = queryBillBy6WH024(query.detail.busiNo);
//            String totalnum = xmlTextReader.getFirstText("/TX/TX_INFO/BkNum1");
            xmlTextWriter.writeElementString("BkListNo1",query.protocolNo);
//            8 BkLevel1             出票人信用等级       varChar3) T 　
            xmlTextWriter.writeElementString("BkLevel1",query.creditLv);
//            9 Bk60Name2            出票人评级机构 varChar(120) T 　
            xmlTextWriter.writeElementString("Bk60Name2",query.gradeAgent);
//            10 Bk8Date3             出票人评级到期日 varChar(8) T 　
            String gradeDueDat = StringHelper.hasAnyChar(query.gradeDueDate)?
                    query.gradeDueDate.substring(0,10).replace("-",""):"";
            xmlTextWriter.writeElementString("Bk8Date3",gradeDueDat);
//            11 SIGN_INFO 证书签名 varChar(254) T 　
            xmlTextWriter.writeElementString("SIGN_INFO","");
//            12 BkNum1               出票循环数           int 　 　
            xmlTextWriter.writeElementString("BkNum1",query.totalNum);
//            循环信息开始
            xmlTextWriter.writeStartElement("DETAILLIST");
            while (query.readRecord()) {
                xmlTextWriter.writeStartElement("DETAILINFO");
//            13 BkListNo2            CLPM票据编号         varChar(10) F 　
                xmlTextWriter.writeElementString("BkListNo2", query.detail.billNum);
//            14 BkListNo3            交易合同编号         varChar(30) T 　
                xmlTextWriter.writeElementString("BkListNo3", query.detail.contractNo);
//            15 BkListNo4            发票号码             varChar(30) T 　
                xmlTextWriter.writeElementString("BkListNo4", query.detail.invoiceNo);
//            16 BkEcdBatchNo         批次号               varChar(9) T 　
                xmlTextWriter.writeElementString("BkEcdBatchNo", "");
//            17 Bk8Date1             出票日               varChar(8) F 　
                xmlTextWriter.writeElementString("Bk8Date1", query.detail.issDate.substring(0, 10).replace("-", ""));
//            18 Bk8Date2             票据到期日           varChar(8) F 　
                xmlTextWriter.writeElementString("Bk8Date2", query.detail.dueDate.substring(0, 10).replace("-", ""));
//            19 BkDetail2            备注                 varChar(512) T 　
                xmlTextWriter.writeElementString("BkDetail2",query.detail.memo+"_"+query.detail.reqSeqID);
//            20 BkType1              不得转让标记 int F EM00可再转让 EM01不得转让
                xmlTextWriter.writeElementString("BkType1", "1".equals(query.detail.transFlag)?"EM00":"EM01");
                xmlTextWriter.writeEndElement();
            }
            xmlTextWriter.writeEndDocument();
            String [] result = sendToBank(xmlTextWriter.toXmlString());
            if(result[0].length() ==0){
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1],tmpsbMsg);
                if(tmpBkReader!=null){
                    String returnCode = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                    //测试
                    //returnCode= "000000";
                    if(StringHelper.hasAnyChar(returnCode )&&"000000".equals(returnCode)) {
                        detail.respCode = "0";
                        detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                        detail.reqSeqID =query.reqBatchNo;

                        detail.transState = returnCode;
                        detail.payInfoCode = detail.transState;
//                        XmlTextWriter applyXml= new XmlTextWriter();
                        //编写查询报文的头部
//                        writeApplyHeader(applyXml);
//                        writeApplyDetail(applyXml,query);
//                        applyXml.writeEndDocument();
//                        String tmpDateDir = Format.DateForLogFormat.format(new Date()).replace("-", File.separator);
//                        String mpSubFileName = FGBiz.dspCenter.bankLogsFirstDir + File.separator + tmpDateDir + File.separator + this.reqHeader.bankCode + File.separator + "B010" + "_协议【" + query.protocolNo+"】出票结果查询模板"+"_"+Format.DateTime14Format.format(new Date()) + ".txt";
//                        String tmpLogFile = FGBiz.dspCenter.bankLogsDirPath + File.separator + mpSubFileName;
//                        FileHelper.writeFile(tmpLogFile, applyXml.getSbXml());

                    }else {
                        detail.respCode = "0";
                        detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                        detail.reqSeqID =query.detail.reqSeqID;
                        detail.transState = returnCode;
                        detail.payInfoCode = detail.transState;
                    }
                }else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpsbMsg.toString();
                }
            }else{
                detail.respCode = "-1";
                detail.respInfo = result[1];
            }
        }catch (Exception ex){
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB119BillRelePledSubmit D_B119_BillResp_Submit(ReqB119BillRelePledSubmit query) throws Exception {
        RetAtsB119BillRelePledSubmit retAts = new RetAtsB119BillRelePledSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        query.readRecord();
        RetAtsB119BillRelePledSubmitDetail detail = new RetAtsB119BillRelePledSubmitDetail();
        try {
            CCB02Header header = new CCB02Header();
            header.REQUEST_SN =getQryTransID();
            header.TX_CODE = "6WH010";
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            writeCommonHeader(xmlTextWriter,header);
            xmlTextWriter.writeStartElement("TX_INFO");
//            7 BkListNo1            电子票据号码         varChar(30) F 　
            xmlTextWriter.writeElementString("BkListNo1",query.detail.billNo );
//            8 ecdsType             电票类型             varChar(4) F AC01:银承；AC02:商承
            xmlTextWriter.writeElementString("ecdsType","1".equals(query.detail.billType)?"AC01":"AC02" );
//            10 Bk60Name1            质权人名称           varChar(120) F 　
            xmlTextWriter.writeElementString("Bk60Name1", query.detail.pledgeeName);
//            11 BkAcctNo1            质权人账号          varChar(32) F 必须与原质押报文中的质权人一致
            xmlTextWriter.writeElementString("BkAcctNo1", query.detail.actNum);
//            12 BkBrchNo1            质权人开户行行号     varChar(12) F 必须与原质押报文中的质权人一致
            xmlTextWriter.writeElementString("BkBrchNo1", query.detail.pledgeeBnkCode);
//            15 BkDetail1            质权人备注           varChar(512) T 　
            xmlTextWriter.writeElementString("BkDetail1",query.detail.memo+"_"+query.detail.reqSeqID );
//            20 BkHvpBrchNo          大小额行号 (联行号)          varChar(12) F 大额行号
            xmlTextWriter.writeElementString("BkHvpBrchNo",query.detail.bigPayBnkCode );// TODO: 2019/8/14
//            24 BkEcdBatchNo         批次号               varChar(10) T 　
            xmlTextWriter.writeElementString("BkEcdBatchNo","" );
//            28 BkElctSgn1           质权人电子签名       varChar(4000) F 　
            xmlTextWriter.writeElementString("BkElctSgn1",query.detail.actNum );
            xmlTextWriter.writeEndElement();
//                28 SIGN_INFO 证书签名 varChar(254) T 　
            xmlTextWriter.writeElementString("SIGN_INFO","");
            xmlTextWriter.writeEndDocument();
            String [] result = sendToBank(xmlTextWriter.toXmlString());
            if(result[0].length() ==0){
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1],tmpsbMsg);
                if(tmpBkReader!=null){
                    String returnCode = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                        detail.respCode = "0";
                        detail.respInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                        detail.reqSeqID =query.detail.reqSeqID;
                        detail.transState = returnCode;
                        detail.payInfoCode = detail.transState;
                }else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpsbMsg.toString();
                }
            }else{
                detail.respCode = "-1";
                detail.respInfo = result[1];
            }
        }catch (Exception ex){
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    private void writeApplyDetail(XmlTextWriter applyXml, ReqB113ProtocolSubmit query) {
        applyXml.writeStartElement("DetailRecord");
        applyXml.writeElementString("ActNum","");
        applyXml.writeElementString("ActArea","");
        applyXml.writeElementString("ProtocolNo",query.protocolNo);
        applyXml.writeElementString("BillCur","");
        applyXml.writeElementString("BillType","1");
        applyXml.writeElementString("BusiType","1");
        applyXml.writeElementString("OperType","1");
        applyXml.writeElementString("OverdueFlag","");
        applyXml.writeElementString("OperateDate",Format.DateTime21Format.format(new Date()));
        applyXml.writeEndElement();
    }

    private void writeApplyHeader(XmlTextWriter applyXml) {
            applyXml.writeStartDocument();
            applyXml.writeStartElement("ATSYH");
            applyXml.writeStartElement("TransReq");
            applyXml.writeElementString("BankCode", this.reqHeader.bankCode);
            applyXml.writeElementString("Entity", this.reqHeader.corporation);
            applyXml.writeElementString("SystemCode", "");
            applyXml.writeElementString("TransType", "B010");
            applyXml.writeElementString("TransSeqID", getQryTransID());
            applyXml.writeElementString("TransTime", Format.DateTimeFormat.format(new Date()));
            applyXml.writeStartElement("TransParam");
    }

    /**
     *  获取票据信息
     * @param actNum
     * @param billType
     * @param billNo
     * @param queryType
     * @return
     */
    private XElement getBillInfoByOwn(String actNum, String billType, String billNo, String queryType)throws Exception {
        XmlTextWriter xmlTextWriter = new XmlTextWriter();
        CCB02Header header = new CCB02Header();
        header.REQUEST_SN = getQryTransID();
        header.TX_CODE = "6WH027";
        writeCommonHeader(xmlTextWriter, header);
        xmlTextWriter.writeStartElement("TX_INFO");
//                    7 ECTYPEVALUE          电票类型              varChar(4) F AC01银承,AC02商承
        if ("1".equals(billType)) {
            billType = "AC01";
        } else {
            billType = "AC02";
        }
        xmlTextWriter.writeElementString("ECTYPEVALUE", billType);
//              8 BkTranType 交易类型 varChar(4) F 0001：承兑（在票据类型为：AC02时候，客户可以发起查询）
//              0002：交票/未用退回 0003：退票（只查询出票已经登记的票据）0004：背书 0005：贴现 0006：质押 0007：质押解除
//              0008：提示付款 0009：拒付追索 0010：非拒付追索 0011：追索（包括拒付和非拒付）0012：查验持票查询

        xmlTextWriter.writeElementString("BkTranType", queryType);
//                    9 ECDSNO               汇票号码             varChar(30) T 　
        xmlTextWriter.writeElementString("ECDSNO", billNo);
//                    10 BEGINDATE            票据到期日开始         varChar(8) T YYYYMMDD
        xmlTextWriter.writeElementString("BEGINDATE", "");
//                    11 ENDDATE              票据到期日结束         varChar(8) T YYYYMMDD
        xmlTextWriter.writeElementString("ENDDATE", "");
//                    12 BRCHNO               承兑银行名称 varchar(120) T 　
        xmlTextWriter.writeElementString("BRCHNO", "");
//                    13 BkAmt1 票据金额开始 Number(18,2) T 0或空表示最大值.
        xmlTextWriter.writeElementString("BkAmt1", "");
//                    14 BkAmt2 票据金额结束 Number(18,2) T
        xmlTextWriter.writeElementString("BkAmt2", "");
//                    15 Bk60Name1            出票人名称           varchar(120) T 　
        xmlTextWriter.writeElementString("Bk60Name1", "");
//                    16 Bk8Date3             票据出票日开始       varChar(8) T YYYYMMDD
        xmlTextWriter.writeElementString("Bk8Date3", "");
//                    17 Bk8Date4             票据出票日结束       varChar(8) T YYYYMMDD
        xmlTextWriter.writeElementString("Bk8Date4", "");
//                    18 PAGE                 当前页次             Number(20) T 默认为1
        xmlTextWriter.writeElementString("PAGE", "1");
//                    19 PAGE_SIZE            每页记录数           Number(20) T 默认为200条记录数
        xmlTextWriter.writeElementString("PAGE_SIZE", "200");
//                    20 BkType4            集团划转标志           Char(1) T 区分上手是否为集团本部标志:0001待转集团 0002 集团划转只有是集团票据才上送以上两个标志
        xmlTextWriter.writeElementString("BkType4", "");
//                    21 BkOthName            收款人名称           varchar(120) T
        xmlTextWriter.writeElementString("BkOthName", "");
        xmlTextWriter.writeEndDocument();
        String[] result = sendToBank(xmlTextWriter.toXmlString());
        if (result[0].length() == 0) {
            StringBuilder tmpsbMsg = new StringBuilder();
            XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1], tmpsbMsg);
            if (tmpbkReader != null) {
                String return_code = tmpbkReader.getFirstText("/TX/RETURN_CODE");
                if (StringHelper.hasAnyChar(return_code) && "000000".equals(return_code)) {
                    String totalNum = tmpbkReader.getFirstText("/TX/TX_INFO/BkTotNum");//总数
                    if (!"1".equals(totalNum)) {
                        throw new Exception("返回的数据不唯一！");
                    }

                    if (tmpbkReader.read("/TX/TX_INFO/DETAILLIST/DETAILINFO")) {
                        return tmpbkReader.readedXElement;
                    }
                } else {
                    throw new Exception(tmpbkReader.getFirstText("/TX/RETURN_MSG"));
                }
            } else {
                throw new Exception(tmpbkReader.toString());

            }
        } else {
            throw new Exception(result[1]);
        }
        return null;
    }

    private XmlTextReader queryBillBy6WH024(String protocol)throws Exception{
        XmlTextWriter xmlWriter = new XmlTextWriter();
        CCB02Header header = new CCB02Header();
        header.REQUEST_SN = getQryTransID();
        header.TX_CODE = "6WH024";
        writeCommonHeader(xmlWriter,header);
        xmlWriter.writeStartElement("TX_INFO");
//            BkFlag1 功能号 varChar(1) F 0:查询正常协议 1:查询失效协议
        xmlWriter.writeElementString("BkFlag1","0");
//            BkListNo1 协议编号 varChar(40) F CLPM合同编号
        xmlWriter.writeElementString("BkListNo1",protocol);
        xmlWriter.writeEndDocument();
        String[] bankResult = sendToBank(xmlWriter.toXmlString());
        if(bankResult[0].length()==0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpbkReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
            if (tmpbkReader != null) {
                String return_code = tmpbkReader.getFirstText("/TX/RETURN_CODE");
                String return_msg = tmpbkReader.getFirstText("/TX/RETURN_MSG");
                if(StringHelper.hasAnyChar(return_code) && "000000".equals(return_code)){
                    return tmpbkReader;
                }else{
                    throw new Exception(return_msg);
                }
            }else{
                throw new Exception(tmpbkReader.toString());
            }
        }else {
            throw new Exception(bankResult[1]);
        }
    }

}
